package day_01.observer_mode_2;

import day_01.observer_mode.DisplayElement;

import java.util.Observable;
import java.util.Observer;

public class CurrentConditionDisplay implements Observer, DisplayElement {
    private Observable observable;
    private float temp;
    private float humidity;

    public CurrentConditionDisplay(Observable observable) {
        this.observable = observable;
        observable.addObserver(this);
    }

    @Override
    public void display() {
        System.out.println(temp + "℃ " + humidity + "%");
    }

    @Override
    public void update(Observable o, Object arg) {
        System.out.println("被观察者：" + o.getClass().getName());
        if(o instanceof WeatherData){
            WeatherData weatherData = (WeatherData) o;
            this.humidity = weatherData.getHumidity();
            this.temp = weatherData.getTemp();
            display();
        }
    }
}
